package com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.qingyiapp.R;
import com.example.qingyiapp.service.entify.Databean;
import com.example.qingyiapp.service.entify.FindFriend;
import com.example.qingyiapp.service.entify.FindTJBean;
import com.example.qingyiapp.service.entify.FriendSquareBean;
import com.example.qingyiapp.service.entify.FriendXxBean;
import com.example.qingyiapp.service.entify.IntrodutioBean;
import com.example.qingyiapp.service.presenter.PersenterMannaer;
import com.example.qingyiapp.service.view.IChildView;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv2;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv3;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_msg;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Address_data_Activity extends AppCompatActivity implements IChildView {

    private TextView title;
    private ImageView userhead;
    private TextView username;
    private ImageView btn_back;
    private TextView time;
    private Button add;
    private TextView datamsg;
    private ListView lvdata;

    String url = "http://www.iqingyi.com/";
    private String id;
    private JavaBean_msg.DataBean data = new JavaBean_msg.DataBean();
    private String content;
    private Html.ImageGetter imgGetter;

    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_data_);
        initView();

        Intent intent = getIntent();
        id = intent.getStringExtra("pid");
        PersenterMannaer persenterMannaer = new PersenterMannaer(this,url);
        persenterMannaer.onCreate();
        persenterMannaer.getlv4(this);
        persenterMannaer.getAddress_lv4(id);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//
//        imgGetter = new Html.ImageGetter() {
//            public Drawable getDrawable(String source) {
//                Drawable drawable = null;
//                URL url = null;
//                try {
//                    url = new URL(source);
//                    drawable = Drawable.createFromStream(url.openStream(), "img");
//                } catch (Exception e) {
//                    return null;
//                }
//                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
//                        .getIntrinsicHeight());
//                return drawable;
//            }
//        };
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                final Spanned text = Html.fromHtml(content, imgGetter, null);
//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        datamsg.setText(text);
//                    }
//                });
//            }
//        }).start();




    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        title = (TextView) findViewById(R.id.title);
        userhead = (ImageView) findViewById(R.id.userhead);
        username = (TextView) findViewById(R.id.username);
        time = (TextView) findViewById(R.id.time);
        add = (Button) findViewById(R.id.add);
        datamsg = (TextView) findViewById(R.id.datamsg);
        lvdata = (ListView) findViewById(R.id.lvdata);
        btn_back = findViewById(R.id.btn_back);

    }

    @Override
    public void onSuccess(Databean s) {

    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ongetAddress_lvData(JavaBean_lv javaBean_lv) {

    }

    @Override
    public void ongetAddress_lvData2(JavaBean_lv2 javaBean_lv2) {

    }

    @Override
    public void ongetAddress_lvData3(JavaBean_lv3 javaBean_lv3) {

    }


    @Override
    public void ongetAddress_lvData4(JavaBean_msg javaBean_lv4) {
        data = javaBean_lv4.getData();
        Glide.with(this).load(data.getUsercover()).apply(RequestOptions.circleCropTransform()).into(userhead);
        String user_name = data.getUser_name();
        username.setText(user_name);
        content = data.getContent();
//        datamsg.setText(Html.fromHtml(content, imgGetter, null));
        String titles = data.getTitle();
        title.setText(titles);
        String times = data.getTime();
        time.setText(times);
        initImageText(content,datamsg);


    }


    @Override
    public void ongetfriendSquareBean(FriendSquareBean friendSquareBean) {

    }

    @Override
    public void ongetfindfriend(FindFriend findFriend) {

    }

    @Override
    public void getjsonstring(FindTJBean s) {

    }

    @Override
    public void getfriendxxbean(FriendXxBean friendXxBean) {

    }

    @Override
    public void getIntrodution1(IntrodutioBean introdutioBean) {

    }

    private void initImageText(String str, final TextView textView) {
        textView.setText(Html.fromHtml(str, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                LevelListDrawable d = new LevelListDrawable();
                Drawable empty = getResources().getDrawable(R.mipmap.abc_list_selector_disabled_holo_dark);
                d.addLevel(0, 0, empty);
                d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

                new LoadImage(textView).execute(source, d);

                return d;
            }
        }, null));

    }

    class LoadImage extends AsyncTask<Object, Void, Bitmap> {

        private TextView textView;
        public LoadImage(TextView textView){
            this.textView = textView;
        }

        private LevelListDrawable mDrawable;

        @Override
        protected Bitmap doInBackground(Object... params) {
            String source = (String) params[0];
            mDrawable = (LevelListDrawable) params[1];
//            Log.d("###", "doInBackground " + source);
            try {
                InputStream is = new URL(source).openStream();
                return BitmapFactory.decodeStream(is);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
//            Log.d("###", "onPostExecute drawable " + mDrawable);
//            Log.d("###", "onPostExecute bitmap " + bitmap);
            if (bitmap != null) {
                BitmapDrawable d = new BitmapDrawable(bitmap);
                mDrawable.addLevel(1, 1, d);
                mDrawable.setBounds(0, 0, (int) (bitmap.getWidth() * 1.8), (int) (bitmap.getHeight() * 1.8));
                mDrawable.setLevel(1);
                // i don't know yet a better way to refresh TextView
                // mTv.invalidate() doesn't work as expected
                CharSequence t = textView.getText();
                textView.setText(t);
            }

        }
    }


}
